{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ch07"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 7.1 input(x),意思是打印出x之后等待用户输入一个值\n",
    "\n",
    "message=input(\"Tell me something, and I will repeat it back to you: \")\n",
    "print(message)\n",
    "\n",
    "# 7.1.1 打招呼\n",
    "name=input(\"Please enter your name: \")\n",
    "print(\"Hello, \"+name+\"!\")\n",
    "\n",
    "## 将这个事先需要打印的x先定义出来\n",
    "\n",
    "prompt=\"If you tell us who you are, we can personalize the messages you see.\"\n",
    "prompt+=\"\\nwhat is your first name?\"### 注意这个+=，p=p+w\n",
    "\n",
    "name=input(prompt)\n",
    "print(\"\\nHello, \"+name+\"!\")\n",
    "\n",
    "\n",
    "# 7.1.2 为了之后能够比较大小，输入的值要求是数字，整型int\n",
    "\n",
    "age=input(\"How old are you?\")\n",
    "age=int(age)\n",
    "print(age>=18)##测试一下这个数能不能作为数值用\n",
    "\n",
    "height=input(\"How tall are you, in inches？\")\n",
    "height=int(height)\n",
    "\n",
    "if height>=36:\n",
    "    print('\\nYou are tall enough to ride!')\n",
    "else:\n",
    "    print(\"\\nYou'll be able to ride when you're a litter older.\")\n",
    "\n",
    "# 7.1.3求余数\n",
    "print(4%3)\n",
    "print(5%3)\n",
    "\n",
    "# 奇数偶数判断\n",
    "number=input(\"Enter a number, and I'll tell you if it's even or odd:\")\n",
    "number=int(number)\n",
    "\n",
    "if number % 2==0:\n",
    "    print(\"\\nThe number \"+str(number)+\" is even.\")\n",
    "else:\n",
    "    print(\"\\nThe number \"+str(number)+\" is odd.\")\n",
    "\n",
    "\n",
    "# 7.2.1 while循环\n",
    "current_number=1\n",
    "while current_number<=5:\n",
    "    print(current_number)\n",
    "    current_number+=1\n",
    "\n",
    "# 7.2.2quit就退出\n",
    "prompt=\"\\nTell me something, and I will repeat it back to you:\"\n",
    "prompt+=\"\\nEnter 'quit' to end the program.\"\n",
    "message=''\n",
    "while message != 'quit':\n",
    "    message=input(prompt)\n",
    "    if message!='quit':\n",
    "        print(message)\n",
    "\n",
    "## 用False，Ture来决定是否退出\n",
    "prompt=\"\\nTell me something, and I will repeat it back to you:\"\n",
    "prompt+=\"\\nEnter 'quit' to end the program.\"\n",
    "\n",
    "active=True\n",
    "while active:\n",
    "    message=input(prompt)\n",
    "\n",
    "    if message=='quit':\n",
    "        active=False\n",
    "    else:\n",
    "        print(message)\n",
    "\n",
    "# 用break退出\n",
    "prompt=\"\\nPlease enter the name of a city you have visitied:\"\n",
    "prompt+=\"\\n(Enter 'quit' when you are finished.)\"\n",
    "\n",
    "while True:\n",
    "    city=input(prompt)\n",
    "    if city == 'quit':\n",
    "        break\n",
    "    else:\n",
    "        print(\"I'd love to go to \"+city.title()+\"!\")\n",
    "\n",
    "\n",
    "#continue 忽略之后的代码\n",
    "current_number=0\n",
    "while current_number<10:\n",
    "    current_number+=1\n",
    "    if current_number % 2 ==0:\n",
    "        continue\n",
    "    print(current_number)\n",
    "\n",
    "# 7.3.1 从一个列表，转移到另一列表\n",
    "unconfirmed_users=['alice','brian','candace']\n",
    "confirmed_users=[]\n",
    "while unconfirmed_users:\n",
    "    current_user=unconfirmed_users.pop()\n",
    "\n",
    "    print(\"Verifying user: \"+current_user.title())\n",
    "    confirmed_users.append(current_user)\n",
    "print(\"\\nThe following users have been confirmed:\")\n",
    "for confirmed_user in confirmed_users:\n",
    "    print(confirmed_user.title())\n",
    "\n",
    "# 7.3.2 删除列表中的所有某个元素\n",
    "\n",
    "pets=['a','b','c','d','c','c']\n",
    "print(pets)\n",
    "\n",
    "while 'c' in pets:\n",
    "    pets.remove('c')\n",
    "\n",
    "print(pets)\n",
    "\n",
    "\n",
    "# 7.3.3用户输入字典\n",
    "responses={}\n",
    "polling_active=True\n",
    "\n",
    "while polling_active:\n",
    "    name=input(\"\\nWhat is your name?\")\n",
    "    response=input(\"Which mountain would you like to climb someday?\")\n",
    "\n",
    "    responses[name]=response\n",
    "    repeat=input(\"Would you like to let another person respond?(yes/no)\")\n",
    "    if repeat=='no':\n",
    "        polling_active=False\n",
    "\n",
    "print(\"\\n---Poll Results---\")\n",
    "for name, response in responses.items():\n",
    "    print(name+' would like to climb '+response+\".\")\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "if height>=36:\n",
    "    print(\"\\nYou're tall enough to ride!\")\n",
    "else:\n",
    "    print(\"\\nYou'll be able to ride when you're a little older.\")\n",
    "\n",
    "\n",
    "\n",
    "#7.3.3 用户输入字典\n",
    "responses={}\n",
    "polling_active=True\n",
    "\n",
    "while polling_active:\n",
    "    name=input(\"\\nWhat is your name?\")\n",
    "    response=input(\"Which mountain would you like to climb someday?\")\n",
    "\n",
    "    responses[name]=response\n",
    "    repeat=input(\"Would you like to let another person respond?(yes/no)\")\n",
    "    if repeat=='no':\n",
    "        polling_active=False\n",
    "\n",
    "print(\"\\n---Poll Results---\")\n",
    "for name, response in responses.items():\n",
    "    print(name+' would like to climb '+response+\".\")\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 习题7-1\n",
    "\n",
    "vehicle=input(\"What kind of vehicle would you like to try?\")\n",
    "\n",
    "print('Ok, I will show you the series of '\n",
    "      +vehicle\n",
    "      +', this way plese.')\n",
    "\n",
    "# 习题7-2 输入数字，加入判断\n",
    "number=input(\"Vous avez combien de personne?\")\n",
    "number=int(number)\n",
    "if number>=8:\n",
    "    print(\"Je suis desole, il n'y a pas assez de place.\")\n",
    "else:\n",
    "    print(\"Par ici, s'il vous plait.\")\n",
    "\n",
    "## 习题7-3 10的整数倍\n",
    "question='Tell me a number.'\n",
    "question+='\\nThen I will show you if it is multiple of ten.'\n",
    "number=input(question)\n",
    "\n",
    "number=int(number)  ##这个转换必须有！！！\n",
    "\n",
    "if number % 10 == 0:\n",
    "    print('Yes,it is!')\n",
    "else:\n",
    "    print('No, it is not.')\n",
    "\n",
    "# 7-4\n",
    "\n",
    "print('输入你想要的配料，输入quit退出')\n",
    "topping=''\n",
    "while topping != 'quit': #用中文输入法输入符号，会有红色下划线\n",
    "    topping=input(\"What would you like to add on you pizza?\")\n",
    "    if topping != 'quit':\n",
    "        print('add some '+topping)\n",
    "\n",
    "# 7-5 while询问年龄 见7-6\n",
    "\n",
    "\n",
    "\n",
    "# 7-6 三个出口\n",
    "\n",
    "## 1while条件测试结束循环\n",
    "\n",
    "## 判断退出，''定义一个空变量，\n",
    "## 当负负得正的时候，实行while，input，\n",
    "## 如果不等于quit打印，打印，继续循环\n",
    "## 等于quit不打印，循环结束\n",
    "\n",
    "print('输入年龄，得到票价，*号退出')\n",
    "age=''\n",
    "\n",
    "while age!='*':\n",
    "    age=input('How old are you？')\n",
    "    if age != '*':\n",
    "        age=int(age)\n",
    "        if age<0:\n",
    "            print('别闹，重新输入')\n",
    "            continue\n",
    "        elif age>=0 and age<3:\n",
    "            price=0\n",
    "        elif age>=3 and age<=12:\n",
    "            price=10\n",
    "        elif age>12:\n",
    "            price=15\n",
    "        print('You admission cost is '+price.__str__()+'$.')\n",
    "\n",
    "print('输入年龄，得到票价，*号退出')\n",
    "\n",
    "## 2active测试循环\n",
    "\n",
    "active = True\n",
    "\n",
    "while active:\n",
    "    age=input('How old are you？')\n",
    "    if age != '*':\n",
    "        age=int(age)\n",
    "        if age<0:\n",
    "            print('别闹，重新输入')\n",
    "            continue\n",
    "        elif age>=0 and age<3:\n",
    "            price=0\n",
    "        elif age>=3 and age<=12:\n",
    "            price=10\n",
    "        elif age>12:\n",
    "            price=15\n",
    "        print('You admission cost is '+price.__str__()+'$.')\n",
    "        ## 这里必须有.str\n",
    "    else:\n",
    "        active = False\n",
    "\n",
    "## break\n",
    "\n",
    "\n",
    "while True:  ### 这里直接用True\n",
    "    age=input('How old are you？')\n",
    "    if age != '*':\n",
    "        age=int(age)\n",
    "        if age<0:\n",
    "            print('别闹，重新输入')\n",
    "            continue\n",
    "        elif age>=0 and age<3:\n",
    "            price=0\n",
    "        elif age>=3 and age<=12:\n",
    "            price=10\n",
    "        elif age>12:\n",
    "            price=15\n",
    "        print('You admission cost is '+price.__str__()+'$.')\n",
    "        ## 这里必须有.str\n",
    "    else:\n",
    "        break\n",
    "\n",
    "# 7-7 无线循环\n",
    "# n=1\n",
    "# while True:\n",
    "#     print(n)\n",
    "#     n+=1\n",
    "\n",
    "# 7-8 熟食店\n",
    "\n",
    "sandwich_orders=['蛋黄三明治','奶酪火腿三明治','三文鱼三明治']\n",
    "finished_sandwiches=[]\n",
    "\n",
    "while sandwich_orders:\n",
    "    sandwich=sandwich_orders.pop()\n",
    "    finished_sandwiches.append(sandwich)\n",
    "\n",
    "print('finished sandwiches:')\n",
    "for sandwich in finished_sandwiches:\n",
    "    print(sandwich)\n",
    "\n",
    "# 习题 7-9 五香牛肉\n",
    "\n",
    "sandwich_orders=['五香牛肉','蛋黄','奶酪','三文鱼','五香牛肉','五香牛肉']\n",
    "\n",
    "while '五香牛肉' in sandwich_orders:\n",
    "    sandwich_orders.remove('五香牛肉')\n",
    "\n",
    "for sandwich in sandwich_orders:\n",
    "    print(sandwich)\n",
    "\n",
    "# 习题 7-10\n",
    "\n",
    "name_places={}\n",
    "\n",
    "print('问卷调查，任何时候，打\"你奏凯\"退出')\n",
    "\n",
    "while True:\n",
    "    name=input(\"您的名字\")\n",
    "    if name == '你奏凯':\n",
    "        break\n",
    "    else:\n",
    "        place=input(\"您最想去的地方？\")\n",
    "        if place=='你奏凯':\n",
    "            break\n",
    "        else:\n",
    "            name_places[name]=place\n",
    "\n",
    "print('调查结束，结果为：')\n",
    "\n",
    "for name, place in name_places.items():\n",
    "    print(name+'最想去的地方:'+place+'。')\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
